package com.news.json;

import java.util.Date;
import java.util.List;
import java.lang.reflect.Field;
import java.util.Arrays;

import org.news.entity.Dept;
import org.news.entity.Emp;
import org.news.entity.User;
//模拟jackson序列化对象
public class Test {
	public static void main(String[] args) throws Exception {
		User user1 = new User("1001", "张无忌", "123456");
		User user2 = new User("1002", "赵敏", "222222");
		User user3 = new User("1003", "小昭", "33333");
		List<User> list = Arrays.asList(user1,user2,user3);
		String json = test2(list);
		System.out.println(json);
	}
	//3.对象中还包含对象  
	// Emp  emp = new Emp(); Dept dept = new Dept(),  emp.setDept();
	public static void test3() throws Exception {
		//基本类型
		List base = Arrays.asList(int.class,byte.class,short.class,long.class,boolean.class,char.class,double.class,float.class,String.class,Date.class);
		Dept dept = new Dept();
		dept.setDeptno(1000);
		dept.setDname("市场部");
		Emp emp = new Emp(1111,"李洁洁",new Date(),dept);//嵌套对象dept
		Class clazz = emp.getClass();
		StringBuffer buffer = new StringBuffer("{");
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			boolean b = base.contains(field.getType());
			if(b){
				buffer.append("\""+field.getName() +"\":\"" +field.get(emp)+"\",");
			}else{
				//当循环到Dept
				Class clazzDept = field.getType();
				Object objDept = field.get(emp);
				//获取Dept的所有属性
				Field[] dFields = clazzDept.getDeclaredFields();
				StringBuffer b2 = new StringBuffer("\"" + field.getName() +"\":{");
				for (Field field2 : dFields) {
					field2.setAccessible(true);
					b2.append("\""+field2.getName() +"\":\"" +field2.get(objDept)+"\",");
				}
				b2.deleteCharAt(b2.length()-1);
				b2.append("}");
				buffer.append(b2);
			}
		}
		buffer.append("}");
		System.out.println(buffer);
	}
	
	
	//2.集合对象 list 或 map
	private static String test2( List<?>  list) throws Exception {
		System.out.println("集合元素的的类型:"+list.getClass());
		StringBuffer buffer = new StringBuffer("[");
		//获取对象的字节码文件 Class对象
		Class clazz = null;
		if(list.size() >0){
			clazz = list.get(0).getClass();
		}
		if(clazz !=null){
			//循环获取每个对象
			//1.获取所有的属性
			Field[] fields = clazz.getDeclaredFields();
			for(Object o: list){
				buffer.append("{");
				for (Field field : fields) {
					   field.setAccessible(true);
						buffer.append("\""+ field.getName() +"\":");
						buffer.append("\"" +field.get(o) + "\",");
				}
				buffer.deleteCharAt(buffer.length()-1);
				buffer.append("},");
			}
			buffer.deleteCharAt(buffer.length()-1);
		}
		buffer.append("]");
		return buffer.toString();
	}
	//1.普通对象处理
	private static void test1() throws IllegalAccessException {
		User user = new User("1001", "张无忌", "123456");
		//{"uname":"张无忌",}
		StringBuffer buffer = new StringBuffer("{");
		//反射
		Class clazz =user.getClass();
		//1.获取所有的属性
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);//强制访问
			buffer.append("\"" + field.getName() + "\":" + "\"" + field.get(user)+ "\",");
			System.out.println(field.getName() + "-->"+ field.get(user));
		}
		buffer.deleteCharAt(  buffer.length()-1 );
		buffer.append("}");
		System.out.println(buffer.toString());
	}

}
